﻿using System;
using System.Collections.Generic;

namespace FFmpegVideoClip.Lib
{
    public static class CheckDuplicateEx
    {
        public static bool CheckDuplicate<TSource>(this IEnumerable<TSource> source)
        {
            return CheckDuplicate(source, null!);
        }
        public static bool CheckDuplicate<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
        {
            Set<TSource> set = new Set<TSource>(comparer);
            foreach (TSource element in source)
            {
                if (!set.Add(element))
                    return true;
            }
            return false;
        }
        public static bool CheckDuplicate<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
        {
            Set<TKey> set = new Set<TKey>(comparer);
            foreach (TSource element in source)
            {
                if (!set.Add(keySelector(element)))
                    return true;
            }
            return false;
        }
    }
}
